package com.example.action;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Servlet implementation class DisplayImageServlet
 */
@WebServlet("/DisplayImageServlet")
public class DisplayImageServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String file = request.getParameter("src");
		response.setContentType("image/jpeg");
		ServletOutputStream out= response.getOutputStream();
		BufferedInputStream bin = new BufferedInputStream(Files.newInputStream(Paths.get("d:/photo/" + file)));
		BufferedOutputStream bout = new BufferedOutputStream(out);
		int ch = 0;
		System.out.println(bin.read());
		while ((ch = bin.read()) != -1) {
			bout.write(ch);
		}		
		bin.close();
		bout.close();
		out.close();
	}
}
